﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using DataAccess.BusinessObject;
using DataAccess.DataObject;
using System.Collections.Generic;
using System.Globalization;
using System.Threading;
using System.Resources;

namespace EnmineKaitlin
{
    public partial class category : Common.PageBase
    {
        ResourceManager rm;
        CultureInfo ci;
        public int CatID = 0;
        public int PostID = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                try
                {                    
                    if (Request.Params["cat"] != null)
                    {
                        string strcat = Request.Params["cat"].ToString();
                        strcat = strcat.Substring(strcat.LastIndexOf("-")+1);
                        CatID = int.Parse(strcat);
                    }
                    if (CatID > 0)
                    {
                        if (Request.Params["p"] != null && Request.Params["p"].ToString()!="")
                        {
                            string strpost = Request.Params["p"].ToString();
                            strpost = strpost.Substring(strpost.LastIndexOf("-") + 1);
                            PostID = int.Parse(strpost);                            
                        }
                        BindPage(CatID,PostID);
                    }
                }catch(Exception ex){
                    Response.Redirect("~/home");
                }                
            }
        }
        protected void BindPage(int CatID,int PostID)
        {
            IList<OCategory> lstCategory = _BCategory.Get(CatID,"vn");
            IList<OCategory> lstCatParent;
            int CatParent=lstCategory[0].CatParent;            
            IList<OCategory> lstCatChild = _BCategory.Get(0, CatID,"vn");
            IList<OPost> lstPost;
            //Load ảnh đại diện của chuyên mục
            if (CatParent > 0)
            {
                //lstCatParent = _BCategory.Get(lstCategory[0].CatParent,"vn");
                //lblCatName.Text = lstCatParent[0].CatName;
                //imgBanner.ImageUrl = lstCatParent[0].FeaturedImage;
                //imgBanner.DataBind();
                ////Bind sidebar leFt
                //rptListCat.DataSource = _BCategory.Get(0, lstCatParent[0].CatID,"vn");
                //rptListCat.DataBind();
                //Bind content
                if (PostID > 0)
                {
                    lstPost = _BPost.Get(PostID);
                    if (lstPost.Count > 0)
                    {
                        rptContentNews.DataSource = lstPost;
                        rptContentNews.DataBind();                                                
                    }
                }
                else {
                    lstPost = _BPost.Get(lstCategory[0].CatID, 20);
                    if (lstPost.Count > 0)
                    {
                        rptListNews.DataSource = lstPost;
                        rptListNews.DataBind();
                    }
                }
            }
            else {
                //lblCatName.Text = lstCategory[0].CatName;
                //imgBanner.ImageUrl = lstCategory[0].FeaturedImage;               
                //imgBanner.DataBind();
                if (lstCatChild.Count > 0)
                {
                    //rptListCat.DataSource = lstCatChild;
                    //rptListCat.DataBind();
                    //Bind content
                    if (PostID > 0)
                    {
                        lstPost = _BPost.Get(PostID);
                        if (lstPost.Count > 0)
                        {
                            rptContentNews.DataSource = lstPost;
                            rptContentNews.DataBind();
                        }
                    }
                    else
                    {
                        lstPost = _BPost.Get(lstCatChild[0].CatID, 20);
                        if (lstPost.Count > 0)
                        {
                            rptListNews.DataSource = lstPost;
                            rptListNews.DataBind();
                        }
                    }
                }
                else {
                    //rptListLeft.DataSource = _BPost.Get(CatID, 100);
                    //rptListLeft.DataBind();
                    //Bind content
                    if (PostID > 0)
                    {
                        lstPost = _BPost.Get(PostID);
                        if (lstPost.Count > 0)
                        {
                            rptContentNews.DataSource = lstPost;
                            rptContentNews.DataBind();
                        }
                    }
                    else
                    {
                        lstPost = _BPost.Get(CatID,1);                        
                        if (lstPost.Count > 0)
                        {
                            rptContentNews.DataSource = lstPost;
                            rptContentNews.DataBind();
                        }
                    }
                }
            }                                    
            //lblUserOnline.Text = Application["useronline"].ToString();
            //lblVisit.Text = Application["count_visit"].ToString();
        }
        protected string LoadString(string method)
        {
            Thread.CurrentThread.CurrentCulture = new CultureInfo(Session["LangID"].ToString());
            rm = new ResourceManager("Resources.Language", System.Reflection.Assembly.Load("App_GlobalResources"));
            ci = Thread.CurrentThread.CurrentCulture;
            switch (method)
            { 
                case "email":
                    return rm.GetString("send_email_post", ci);
                    break;
                case "save":
                    return rm.GetString("save_post", ci);
                    break;
                case "print":
                    return rm.GetString("print_post", ci);
                    break;
            }
            return "";
        }
    }
}
